tempfile temp1 temp2 temp3
// Constructs an income panel for couples with DV report 
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results"


import sas using "D:\c55\custom-made\u1067_al3\u1067_al3_vanhempi_lapsi.sas7bdat", clear
count if isa_aiti==""

*keep biological parent and children 
keep if (isa_aiti=="1" | isa_aiti=="2")

gen byc=substr(syntykk_lapsi, 1,4)

destring byc, replace 


sort shnro_v byc

bysort shnro_v: gen child_id=_n

sort shnro_v byc


* shnro_v mother or father id; shnro_l child_id 
duplicates report shnro_l shnro_v
duplicates report shnro_l


preserve 
* keep only mother 
keep if isa_aiti=="2"
duplicates drop shnro_l, force 


keep shnro_l shnro_v byc 

rename shnro_l shnro_sec
rename shnro_v shnro_first 
rename byc byc_sec 

save "$data_folder_final\link_mother.dta", replace 

use shnro sukup kieli ptoim1 ika using "D:\ready-made\FOLK_perus_8800a\folk_19872000_tua_perus16tot_1.dta", clear
keep if ika==35
keep if sukup=="2"
gen mother_employed=ptoim1=="11"
keep shnro mother_employed
*merge mother id from link files
duplicates drop shnro, force
rename shnro shnro_first
sort shnro_first
merge 1:m shnro_first using "$data_folder_final\link_mother.dta", keepusing(shnro_sec)
tab _merge
drop if _merge==2 
duplicates drop shnro_sec, force
keep shnro_sec mother_employed
save `temp1'

use shnro sukup kieli ptoim1 ika using "D:\ready-made\FOLK_perus_0110a\folk_20012010_tua_perus21tot_1.dta" , clear
keep if ika==35
keep if sukup=="2"
gen mother_employed=ptoim1=="11"
keep shnro mother_employed
*merge mother id from link files
duplicates drop shnro, force
rename shnro shnro_first
sort shnro_first
merge 1:m shnro_first using "$data_folder_final\fir_sec_gen_id_link_mother.dta", keepusing(shnro_sec)
tab _merge
drop if _merge==2 
duplicates drop shnro_sec, force
keep shnro_sec mother_employed
save `temp2'


use shnro sukup kieli ptoim1 ika using "D:\ready-made\FOLK_perus_11a\folk_20112020_tua_perus22tot_1.dta"
keep if ika==35
keep if sukup=="2"
gen mother_employed=ptoim1=="11"
keep shnro mother_employed
*merge mother id from link files
duplicates drop shnro, force
rename shnro shnro_first
sort shnro_first
merge 1:m shnro_first using "$data_folder_final\fir_sec_gen_id_link_mother.dta", keepusing(shnro_sec)
tab _merge
drop if _merge==2 
duplicates drop shnro_sec, force
keep shnro_sec mother_employed
save `temp3'

use  `temp1'
append using `temp2'
append using `temp3'

drop if shnro_sec==""
duplicates drop shnro_sec, force
sort shnro_sec

save "$data_folder_final\mother_education.dta", replace 



